<template>
	<om-page title="教育经历">
			<uni-card :titleUnderline="false" :is-full="true" style="border: none;border-radius: 1px;" titleBgColor="#eee"
			 :title="'教育经历(' + order + ')'">
				<uni-list>
					<uni-list-item>
						<view class="tui-flex">
							<view class="tui-col-5">
								<om-label label="开始日期" required></om-label>
							</view>
							<view class="tui-right tui-col-7">
								<picker mode="date" :end="formdata.endDateE" fields="month" @change="bindStartDateChange" :value="formdata.startDateE">
									<view>{{ formdata.startDateE || '请选择' }}</view>
								</picker>
							</view>
						</view>
					</uni-list-item>
					<uni-list-item >
						<view class="tui-flex">
							<view class="tui-col-5">
								<om-label label="结束日期" required></om-label>
							</view>
							<view class="tui-right tui-col-7">
								<picker mode="date" :end="currentDate" fields="month" :start="formdata.startDateE" @change="bindEndDateChange" :data-index='index' :value="formdata.endDateE">
									<view>{{ formdata.endDateE || '请选择' }}</view>
								</picker>
							</view>
						</view>
					</uni-list-item>
					<uni-list-item :showArrow="false">
						<view class="tui-flex">
							<view class="tui-col-5">
								<om-label label="院校名称" required></om-label>
							</view>
							<view class="tui-col-7">
								<input v-model="formdata.schoolE" placeholder="请输入"/>
							</view>
						</view>
					</uni-list-item>
					<uni-list-item>
						<view class="tui-flex">
							<view class="tui-col-5"><om-label label="学历" required></om-label></view>
							<view class="tui-right tui-col-7">
								<picker mode="selector" @change="bindEduChange" :range="eduList" :value="eduIndex" range-key="dicname">
									<view>{{ (eduList.length && ~eduIndex) ? eduList[eduIndex].dicname : '请选择' }}</view>
								</picker>
							</view>
						</view>
					</uni-list-item>
					<uni-list-item>
						<view class="tui-flex">
							<view class="tui-col-5"><om-label label="专业"></om-label></view>
							<view class="tui-right tui-col-7" @click="bindStudyDirectChange">
								<view>{{ formdata.studyDirectEName || '请选择' }}</view>
							</view>
						</view>
					</uni-list-item>
					<uni-list-item>
						<view class="tui-flex">
							<view class="tui-col-5"><om-label label="学习形式"></om-label></view>
							<view class="tui-right tui-col-7">
								<picker mode="selector" @change="bindStudyWayChange" :range="studyWayList" :value="studyWayIndex" range-key="dicname">
									<view>{{ (studyWayList.length && ~studyWayIndex) ? studyWayList[studyWayIndex].dicname : '请选择' }}</view>
								</picker>
							</view>
						</view>
					</uni-list-item>
					<uni-list-item :showArrow="false">
						<view class="tui-flex">
							<view class="tui-col-5">
								<om-label label="证书号"></om-label>
							</view>
							<view class="tui-col-7">
								<input v-model="formdata.certificateE" placeholder="请输入"/>
							</view>
						</view>
					</uni-list-item>
				</uni-list>
			</uni-card>
		<view class="padding">
			<button class="bg-green margin-bottom" @click="save" >确认</button>
			<button v-if="index != null" @click="del" class="bg-grey">删除</button>
		</view>
	</om-page>
</template>

<script>
	import dateTimePicker from '@/static/js/dateTimePicker.js';
	import {subOnLoad} from '../../../static/js/formUtil.js';
	export default {
		data() {
			return {
				cachename:'',
				index:null,
				datas:[],
				order:0,
				formdata:{
					schoolE:"",
					startDateE:'',
					endDateE:'',
					eduEName:'',
					eduECode:'',
					studyDirectEId:'',
					studyDirectEName:'',
					studyWayCode:'',
					studyWayName:'',
					certificateE:'',
				},
				currentDate:dateTimePicker.getNewDate(),
				eduIndex:1,
				eduList:[],//学历
				studyWayIndex:0,
				studyDirectList:[],//专业
				studyWayList:[],//学习形式
				majorCacheKey:"major-cache"
			}
		},
		methods: {
			save(){
				let formdata = this.formdata
				if(!formdata.startDateE){
					this.om.toast("请选择开始时间")
					return
				}
				if(!formdata.endDateE){
					this.om.toast("请选择结束时间")
					return
				}
				if(!formdata.schoolE){
					this.om.toast("请输入毕业院校")
					return
				}
				if(!formdata.eduEName){
					this.om.toast("请输入学历")
					return
				}
				let _that = this
				if(this.index != null){
					Object.assign(this.datas[this.index],this.formdata)
				}else{
					this.datas.push(this.formdata)
				}
				uni.setStorage({
				  key: this.cachename,
				  data: this.datas
				})
				uni.navigateBack()
			},
			del(){
				if(this.index != null){
					this.datas.splice(this.index,1)
				}
				uni.setStorage({
				  key: this.cachename,
				  data: this.datas
				})
				uni.navigateBack()
			},
			bindStartDateChange(e){
				this.formdata.startDateE = e.detail.value
			},
			bindEndDateChange(e){
				this.formdata.endDateE = e.detail.value
			},
			bindEduChange(e){
				if(e){
					this.eduIndex = e.detail.value;
				}
				this.formdata.eduECode= this.eduList[this.eduIndex].code
				this.formdata.eduEName = this.eduList[this.eduIndex].dicname
				this.studyDirectEId = ''
				this.studyDirectEName = ''
			},
			bindStudyDirectChange(e){
				if(!this.formdata.eduECode){
					this.om.toast("请先选择学历")
					return
				}
				if(this.eduIndex < 5){
					this.om.toast("需大专及大专以上学历")
					return
				}
				uni.navigateTo({
					url: '../../public/search/search?cachename='+this.majorCacheKey + "&url="+encodeURIComponent("wxTpiApply/getMajorList.do?eduCode=" + this.formdata.eduECode)
				});
			},
			bindStudyWayChange(e){
				if(e){
					this.studyWayIndex = e.target.value;
				}
				this.formdata.studyWayCode = this.studyWayList[this.studyWayIndex].code
				this.formdata.studyWayName = this.studyWayList[this.studyWayIndex].dicname
			},
			initLoad(){
				let _that = this
				//学历
				_that.om.request('dictZero/getListByCode.do', {
					code: '23868'
				}).then(res => {
					//if (res instanceof Array) {
					if(Object.prototype.toString.call(res)==='[object Array]'){
						_that.eduList = res
						if(_that.formdata.eduECode){
							for(var i=0;i<_that.eduList.length;i++){
								var item = _that.eduList[i]
								if(item.code == _that.formdata.eduECode){
									_that.eduIndex = i
									break;
								}
							}
						}
						_that.bindEduChange()
					}
				});
				//学习形式
				_that.om.request('dictZero/getListByCode.do', {
					code: '34783'
				}).then(res => {
					//if (res instanceof Array) {
					if(Object.prototype.toString.call(res)==='[object Array]'){
						_that.studyWayList = res
						if(_that.formdata.studyWayCode){
							for(var i=0;i<_that.studyWayList.length;i++){
								var item = _that.studyWayList[i]
								if(item.code == _that.formdata.studyWayCode){
									_that.studyWayIndex = i
									break;
								}
							}
						}
						_that.bindStudyWayChange()
					}
				});
				
			}
		},
		onLoad(e){
			subOnLoad(this,e,"initLoad")
		},
		onShow() {
			let _that = this
			uni.getStorage({
				key: _that.majorCacheKey,
				success: function(res) {
					if(res.data){
						_that.formdata.studyDirectEName = res.data.dicname;
						_that.formdata.studyDirectEId = res.data.id;
						uni.removeStorage({
							key: _that.majorCacheKey
						});
					}
				}
			});
		}
	}
</script>

<style>

</style>
